//
//  SettingsView.m
//  iMarks
//
//  Created by Christian Speich on 03.01.08.
//  Copyright 2008 Christian Speich. All rights reserved.
//

#import <UIKit/UIPreferencesControlTableCell.h>
#import <UIKit/UISwitchControl.h>
#import "SettingsView.h"


@implementation SettingsView

- (id)initWithFrame:(struct CGRect)frame andMainView:(id)mainView_
{
    self = [super initWithFrame:frame];
    if (self) {
		NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
		navigationBar = [[UINavigationBar alloc] initWithFrame: 
						 CGRectMake(0.0f, 0.0f, frame.size.width,
									[UINavigationBar defaultSize].height)];
		
		navigationItem = [[UINavigationItem alloc]
						  initWithTitle:@"Einstellungen"];
		
		transView = [[UITransitionView alloc] 
					 initWithFrame:CGRectMake(0.0f,
										[UINavigationBar defaultSize].height,
										frame.size.width, frame.size.height-[UINavigationBar defaultSize].height)];
		
		subjectView = [[SubjectView alloc] 
					   initWithFrame:CGRectMake(0.0f,
												[UINavigationBar defaultSize].height,
												frame.size.width, frame.size.height-[UINavigationBar defaultSize].height)
					   andEditMode:YES];
		
		viewManager = [[ViewManager alloc] initWithTransitionView:transView
												 andNavigationBar:navigationBar
													  andMainView:nil];
		
        table = [[UIPreferencesTable alloc] 
				 initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, 
										  frame.size.height-[UINavigationBar defaultSize].height)];
		
		secondHalfYear = [[UIPreferencesControlTableCell alloc] init];
		[secondHalfYear setTitle:
		 NSLocalizedString(@"secondHalfYear",
						   @"Label for the second Half Year")];
		[secondHalfYear setEnabled:NO];
		secondHalfYearControl = [[UISwitchControl alloc] 
								 initWithFrame:CGRectMake(200.0f, 10.0f, 50.0f,
														  20.0f)];
		[secondHalfYearControl setValue:([userDefaults
										  boolForKey:@"iMarksSecondHalfYear"]?
										 1.0f:0.0f)];
		[secondHalfYear setControl:secondHalfYearControl];
		
		changeSubject = [[UIPreferencesTableCell alloc] init];
		[changeSubject setTitle:
		 NSLocalizedString(@"prefChangeSubjects", 
						   @"Buttonlabel for changing Subjects")];
		[changeSubject setShowDisclosure: YES];
		[changeSubject setEnabled:NO];
		
		deleteAllSubjects = [[UIPreferencesTableCell alloc] init];
		[deleteAllSubjects setTitle:
		 NSLocalizedString(@"prefDeleteAllSubjects", 
						   @"Buttonlabel for deleting all Subjects")];
		[deleteAllSubjects setAlignment:2];
		[deleteAllSubjects setEnabled:NO];

		
		deleteAllSubjectsSheet = [[UIAlertSheet alloc] initWithTitle:@""
															 buttons:Nil
												  defaultButtonIndex:0
															delegate:self
															 context:self];
		[deleteAllSubjectsSheet 
		 setDestructiveButton:[deleteAllSubjectsSheet addButtonWithTitle:
							   NSLocalizedString(@"prefDeleteAllSubjects",
												 @"Buttonlabel for deleting all Subjects")]];
		[deleteAllSubjectsSheet setDefaultButton:[deleteAllSubjectsSheet 
												  addButtonWithTitle:@"Cancel"]];
		
		
		deleteAllMarks = [[UIPreferencesTableCell alloc] init];
		[deleteAllMarks setTitle:
		 NSLocalizedString(@"prefDeleteAllMarks", 
						   @"Buttonlabel for deleting all Marks")];
		[deleteAllMarks setAlignment:2];
		[deleteAllMarks setEnabled:NO];
		
		deleteAllMarksSheet = [[UIAlertSheet alloc] initWithTitle:@""
															 buttons:Nil
												  defaultButtonIndex:0
															delegate:self
															 context:self];
		[deleteAllMarksSheet 
		 setDestructiveButton:[deleteAllMarksSheet addButtonWithTitle:
							   NSLocalizedString(@"prefDeleteAllMarks",
												 @"Buttonlabel for deleting all Marks")]];
		[deleteAllMarksSheet setDefaultButton:[deleteAllMarksSheet 
												  addButtonWithTitle:@"Cancel"]];
		
		NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
		
		versionCell = [[UIPreferencesTableCell alloc] init];
		[versionCell setTitle:
		 NSLocalizedString(@"prefVersion", 
						   @"Versions Label")];
		[versionCell setValue:
		 [NSString stringWithFormat:@"%@ (%@)", 
		  [infoPlist objectForKey: @"CFBundleShortVersionString"], 
		  [infoPlist objectForKey: @"CFBundleVersion"]]];
		
		authorCell = [[UIPreferencesTableCell alloc] init];
		[authorCell setTitle:
		 NSLocalizedString(@"prefAuthor", 
						   @"Author Label")];
		[authorCell setValue:@"Christian Speich"];
		
		[table setDataSource:self];
		[table setDelegate:self];
		[table reloadData];
		
		[self addSubview:navigationBar];
		[self addSubview:transView];
		[viewManager addView:table withNavigationItem:navigationItem];
		
		mainView = mainView_;
    }
    return self;
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable*)table
{
	return 3;
}

- (NSString *) preferencesTable:(UIPreferencesTable *) table titleForGroup:(int) group {
	switch (group) {
		case 0: return NSLocalizedString(@"subjects", 
										 @"Subjects Label");
		case 1: return NSLocalizedString(@"marks", 
										 @"Marks Label");
		case 2: return NSLocalizedString(@"infos", 
										 @"Infos Label");
		default: return nil;
	}
}

- (int)preferencesTable:(UIPreferencesTable*)table numberOfRowsInGroup:(int)group
{
	switch (group) {
		case 0: return 2;
		case 1: return 2;
		case 2: return 2;
		default: return 0;
	}
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)_table 
								  cellForRow:(int)row inGroup:(int)group
{
	if (group == 0) {
		switch (row) {
			case 0: return changeSubject;
			case 1: return deleteAllSubjects;
			default: return nil;
		}
	}
	else if (group == 1) {
		switch (row) {
			case 0: return secondHalfYear;
			case 1: return deleteAllMarks;
			default: return nil;
		}
	}
	else if (group == 2) {
		switch (row) {
			case 0: return versionCell;
			case 1: return authorCell;
			default: return nil;
		}
	}
	
	return nil;
}

- (void) save
{
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	
	if ([secondHalfYearControl value]) {
		[userDefaults setBool:YES forKey:@"iMarksSecondHalfYear"];
	}
	else {
		[userDefaults setBool:NO forKey:@"iMarksSecondHalfYear"];
	}
}

- (void) dealloc
{
	[self save];
	
	[navigationBar release];
	[table release];
	[navigationItem release];
	[secondHalfYear release];
	[secondHalfYearControl release];
	[versionCell release];
	[authorCell release];
	
	[super dealloc];
}

- (BOOL) table:(UITable*)_table canSelectRow:(int)_row
{
	int row;
	int group;
	
	[table getGroup:&group row:&row forTableRow:_row];

	if (group == 0) {
		switch (row) {
			case 0:
			case 1: return YES;
			default: break;
		}
	}
	else if (group == 1) {
		switch (row) {
			case 1: return YES;
			default: break;
		}
	}
	
	return NO;
}

- (void)tableRowSelected:(NSNotification *)notification
{	
	int row;
	int group;
	
	[table getGroup:&group row:&row forTableRow:[table selectedRow]];
	
	if (group == 0) {
		switch (row) {
			case 0:
				[viewManager addView:subjectView];
				break;
			case 1:
				[deleteAllSubjectsSheet presentSheetInView:mainView];
				break;
			default:
				break;
		}
	}
	else if (group == 1) {
		switch (row) {
			case 1:
				[deleteAllMarksSheet presentSheetInView:mainView];
				break;
			default:
				break;
		}
	}
	else
		return;
	
	[table selectRow:-1 byExtendingSelection:NO withFade:YES
  scrollingToVisible:NO];
}

- (void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(int)button
{
	if (button == 1) {
		
	}
	[sheet dismiss];
}

@end
